package com.jd.platform.gobrs.client.config;

import com.jd.platform.gobrs.client.ApplicationContextProvider;
import com.jd.platform.gobrs.client.ClientStarter;
import com.jd.platform.gobrs.client.core.key.PushSchedulerStarter;
import com.jd.platform.gobrs.client.dispatcher.GCache;
import com.jd.platform.gobrs.client.dispatcher.GCacheDispatcher;
import com.jd.platform.gobrs.client.enable.AutoDepBean;
import com.jd.platform.gobrs.client.enable.BeforeCheckConfiguration;
import com.jd.platform.gobrs.client.enable.GobrsStarterDepBean;
import com.jd.platform.gobrs.client.enable.ManualDepBean;
import com.jd.platform.gobrs.client.manager.AutoCacheManager;
import com.jd.platform.gobrs.client.manager.ManualCacheManager;
import com.jd.platform.gobrs.client.starter.GobrsBannerHandler;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfiguration;

import javax.annotation.PostConstruct;

/**
 * @program: gobrs
 * @ClassName GobrsAutoConfiguration  auto config
 * @description:
 * @author: sizegang
 * @create: 2022-01-08 18:21
 * @Version 1.0
 **/
@Configuration
@AllArgsConstructor
@ConditionalOnBean(GobrsStarterDepBean.Marker.class)
@EnableConfigurationProperties(BootstrapProperties.class)
@ConditionalOnProperty(prefix = BootstrapProperties.PREFIX, value = "enable", matchIfMissing = true, havingValue = "true")
@ImportAutoConfiguration({})
public class GobrsAutoConfiguration {

    private final BootstrapProperties properties;

    private final ConfigurableEnvironment environment;


    @Bean
    public GobrsBannerHandler gobrsBannerHandler() {
        return new GobrsBannerHandler(properties);
    }

    @ConditionalOnBean(ManualDepBean.ManualMarker.class)
    @Bean
    public ManualCacheManager manualCacheManager() {
        return new ManualCacheManager();
    }

    @ConditionalOnBean(AutoDepBean.AutoMarker.class)
    @Bean
    public AutoCacheManager autoCacheManager() {
        return new AutoCacheManager();
    }


    @Bean
    public ApplicationContextProvider applicationContextProvider() {
        return new ApplicationContextProvider();
    }

    @Bean
    public GCacheDispatcher gCacheDispatcher() {
        return new GCacheDispatcher();
    }

    @Bean
    public GCache gCache() {
        return new GCache();
    }

    @Bean
    public PushSchedulerStarter pushSchedulerStarter() {
        return new PushSchedulerStarter();
    }


    @Bean
    public BeforeCheckConfiguration beforeCheckConfiguration(BootstrapProperties bootstrapProperties, ConfigurableEnvironment configurableEnvironment) {
        return new BeforeCheckConfiguration();
    }

    /**
     * Gobra 核心加载
     */
    @Bean
    public ClientStarter clientStarter() {
        ClientStarter.Builder builder = new ClientStarter.Builder();
        ClientStarter start = builder.setAppName(properties.getAppName()).setEtcdServer(properties.getEtcdServer())
                .setPushPeriod(properties.getPushPeriod()).setCaffeineSize(properties.getCaffeineSize())
                .build();
        return start;
    }

}
